package com.wangsd.web.face.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.web.face.entity.MemberLevel;
import com.wangsd.web.face.mapper.MemberLevelMapper;
import com.wangsd.web.face.service.MemberLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-10-31
 */
@Service
public class MemberLevelServiceImpl extends ServiceImpl<MemberLevelMapper, MemberLevel> implements MemberLevelService {

    @Resource
    MemberLevelMapper memberLevelMapper;

    @Override
    public MemberLevel selectIsDefaultAndMerchantId(Integer isDefault, Long merchantId) {
        QueryWrapper<MemberLevel> queryWrapper = new QueryWrapper<>();
        if (isDefault != null) {
            queryWrapper.eq("is_default", isDefault);
        }
        queryWrapper.eq("merchant_id", merchantId);
        return memberLevelMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer selectCountMerchantId(Integer isDefault, Long merchantId) {
        QueryWrapper<MemberLevel> queryWrapper = new QueryWrapper<>();
        if (isDefault != null) {
            queryWrapper.eq("is_default", isDefault);
        }
        queryWrapper.eq("merchant_id", merchantId);
        return memberLevelMapper.selectCount(queryWrapper);
    }

    @Override
    public IPage<MemberLevel> selectList(Integer pageNum, Integer pageSize, MemberLevel params, String code) {
        Page<MemberLevel> page = new Page<>(pageNum, pageSize);
        IPage<MemberLevel> resultIPage = memberLevelMapper.selectListXml(page, params, code);
        return resultIPage;
    }

    @Override
    @Transactional
    public void addOrUpdate(MemberLevel params) {
        if (params.getId() != null) {
            memberLevelMapper.updateById(params);
        }else{
            memberLevelMapper.insert(params);
        }
    }
}
